<?php
namespace app\admin\controller;
use app\admin\controller\Common;
use app\service\visitorService;
use app\service\websetService;
use app\service\roomService;
use app\service\wotuService;
use app\service\iotmcuService;
use think\Db;
class Visitor extends Common{
    //访客列表
    public function visitorList(){
         //查询小区信息
        $com = $this->pidTokenId();
        $whe['id'] = array('in',$com['community_id']);
        $communitylist = (new roomService())->communityList($whe);
       
        $community_id = $com['community_id'];
        $this->assign('communitylist',$communitylist);
        //查询
        $data = request()->get();
        // dump($data);exit();
        if (!empty($data['community_id'])){
            $groups = (new roomService())->disgroup($data['community_id']);
            $this->assign('groups', $groups);
            // dump($data['group']);
            if (!empty($data['group'])) {
                $buildings = (new roomService())->disbuilding($data['community_id'],$data['group']);
                $this->assign('buildings', $buildings);
                // dump($buildings);exit();
                if (!empty($data['building'])) {
                    $units =  (new roomService())->disunit($data['community_id'], $data['group'],$data['building']);
                    $this->assign('units', $units);
                    if(!empty($data['unit'])){
                        $rooms = (new roomService())->disroom($data['community_id'], $data['group'],$data['building'],$data['unit']);
                        $this->assign('rooms',$rooms);
                    }
                }
            }
        }
        
        $where = [];
        if(!empty($data)){
            if(!empty($data['community_id'])){
                $where['community_id'] = $data['community_id'];//小区名称
            }else{
                $where['community_id'] = "";
            }
            if(!empty($data['group'])){
                $where['group'] = $data['group'];//组团
            }else{
                $where['group'] = "";
            }
            if(!empty($data['building'])){
                $where['building'] = $data['building'];
            }else{
                $where['building'] = "";
            }
            if(!empty($data['unit'])){
                $where['unit'] = $data['unit'];
            }else{
                $where['unit'] = "";
            }
            if(!empty($data['room'])){
                $where['room'] = $data['room'];
            }else{
                $where['room'] = "";
            }
            if(!empty($data['owner_name'])){
                $where['name'] = $data['owner_name'];
            }else{
                $where['name'] = "";
            }
            if(!empty($data['phone'])){
                $where['phone'] = $data['phone'];
            }else{
                $where['phone'] = "";
            }
            
        }
        //die;
       
        
        $where = array_filter($where);
      
        $page = request()->has('page') ? request()->get('page') : 1;
        //查询房屋条数
        $number = (new visitorService())->visitorcount($where,$community_id);
        $this->assign('number',$number);
        //查询房屋信息
        $visitorlist = (new visitorService())->visitorList($where,$page,$community_id);
        
        if(!isset($list)){
            $this->assign('list',$visitorlist);
        }else{
            $this->assign('list',$list);
        }
        $this->assign('visitorlist',$visitorlist);
       
        foreach($data as $k => $v) {
            $this->assign($k, $v);
        }

        
        return $this->fetch('visitor_list');
    }
    
     public function visitordel(){
        $id=request()->get("id");
        
        $userinfo=Db::table("visitor")->where("id",$id)->find();
        if(empty($userinfo)){
            return json(['errMsg' => '该用户不存在']);
        }

    	if($userinfo["personGuid"]!="0"){
            $result=(new wotuService())->del($userinfo["personGuid"]);
            
    	}
        $res=Db::table("visitor")->where("id",$id)->delete();
        if($res){
            $this->success("删除成功");
        }else{
            $this->success("删除失败");
        }
        
        
    }
    
}